from django.contrib import admin
from django.shortcuts import render
from .models.BaseModels import *
from .models.QuanModels import *
from .views.views import qqDataUpload, BaiduCodeUpload, BaiduDataUpload
from .views import quanquaner

# Register your models here.
@admin.register(AdminUser)
class UserAdmin(admin.ModelAdmin):
    list_display=('id', 'openid', 'nickName', 'mobile', 'created_at', 'permission', 'qrcode_colored')
    #list_per_page设置每页显示多少条记录，默认是100条
    list_per_page = 20
    exclude = ('created_at', 'openid')

@admin.register(Log)
class LogAdmin(admin.ModelAdmin):
    list_display=('id', 'openid', 'log', 'created_at')
    #list_per_page设置每页显示多少条记录，默认是100条
    list_per_page = 20

@admin.register(LastdayNum)
class LastdayAdmin(admin.ModelAdmin):
    list_display=('id', 'data_name', 'data_num', 'date')
    #list_per_page设置每页显示多少条记录，默认是100条
    list_per_page = 20

@admin.register(qqData)
class QqAdmin(admin.ModelAdmin):
    list_display = ('id', 'uid', 'date', 'salesId', 'imei', 'phoneBank', 'phoneModel', 'task_colored', 'audit_colored')
    #list_per_page设置每页显示多少条记录，默认是100条
    list_per_page = 20
    list_filter = ('taskStatus', 'auditStatus')  # 过滤器

@admin.register(qqDataUploadPage)
class QqDataUploadAdmin(admin.ModelAdmin):
    def changelist_view(self, request, extra_context=None):
        return qqDataUpload(request)

@admin.register(SalsesTop)
class SalsesTopAdmin(admin.ModelAdmin):
    list_display = ('id', 'userId', 'mobile_hide', 'area', 'order_num', 'qh_coin')
    list_per_page = 20
    ordering = ('-order_num',)

# @admin.register(JiaoShuoShuo)
# class JiaoShouShuoAdmin(admin.ModelAdmin):
#     list_display = ('id', 'column', 'periods', 'title')

@admin.register(BaiduCodeUploadPage)
class BaiduCodeUploadAdmin(admin.ModelAdmin):
    def changelist_view(self, request, extra_context=None):
        return BaiduCodeUpload(request)

@admin.register(BaiduCode)
class BaiduCodeAdmin(admin.ModelAdmin):
    list_display = ('id', 'user_name', 'mobile', 'baidu_url', 'haokan_url', 'status_colored', 'click')
    list_filter = ('status',)  # 过滤器
    search_fields = ('mobile',)  # 搜索字段

    actions = ['updateBatch', 'updateStatus']

    def updateBatch(self, request, queryset):
        for i in queryset.values():
            if i['admin_user_id'] == None or i['admin_user_id'] == '':
                quanquaner.getUserInfo(request, i['id'])
    updateBatch.short_description = "批量更新用户数据"

    def updateStatus(self, request, queryset):
        for i in queryset.values():
            if i['status'] == 0:
                BaiduCode.objects.filter(id=i['id']).update(status=1)
            else:
                BaiduCode.objects.filter(id=i['id']).update(status=0)
    updateStatus.short_description = "更新状态"

@admin.register(Material)
class MaterialAdmin(admin.ModelAdmin):
    list_display = ('id', 'name', 'media_id', 'media_url', 'media_upload')

@admin.register(News)
class NewsAdmin(admin.ModelAdmin):
    list_display = ('id', 'title', 'description', 'media', 'Url')

@admin.register(BaiduDataUploadPage)
class BaiduDataUploadAdmin(admin.ModelAdmin):
    def changelist_view(self, request, extra_context=None):
        return BaiduDataUpload(request)

@admin.register(BaiduOrderData)
class BaiduDataAdmin(admin.ModelAdmin):
    list_display = ('id', 'date', 'admin_user', 'proxy', 'team', 'baidu','valid_num', 'yuzhuang_valid_num')
    list_filter = ('admin_user',)
    search_fields = ('baidu', 'proxy')
    date_hierarchy = 'date'

@admin.register(TmallStoreSn)
class TmallAdmin(admin.ModelAdmin):
    list_display = ('id', 'store', 'store_sn', 'pid', 'click_num')
    search_fields = ('store_sn',)

class MultiDBModelAdmin(admin.ModelAdmin):
    # A handy constant for the name of the alternate database.
    using = 'app_db'

    def save_model(self, request, obj, form, change):
        # Tell Django to save objects to the 'other' database.
        obj.save(using=self.using)

    def delete_model(self, request, obj):
        # Tell Django to delete objects from the 'other' database
        obj.delete(using=self.using)

    def get_queryset(self, request):
        # Tell Django to look for objects on the 'other' database.
        return super(MultiDBModelAdmin, self).get_queryset(request).using(self.using)

    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        # Tell Django to populate ForeignKey widgets using a query
        # on the 'other' database.
        return super(MultiDBModelAdmin, self).formfield_for_foreignkey(db_field, request, using=self.using, **kwargs)

    def formfield_for_manytomany(self, db_field, request, **kwargs):
        # Tell Django to populate ManyToMany widgets using a query
        # on the 'other' database.
        return super(MultiDBModelAdmin, self).formfield_for_manytomany(db_field, request, using=self.using, **kwargs)

@admin.register(JiemuRecords)
class JiemuRecordAdmin(MultiDBModelAdmin):
    list_display = ('id', 'user', 'jiemu', 'create_time')

@admin.register(JiemuOrder)
class JiemuOrderAdmin(MultiDBModelAdmin):
    list_display = ('id', 'user', 'jiemu', 'buy_time', 'pay_method_colored', 'pay_amount')

admin.site.site_header = '全圈数据后台'
admin.site.site_title = '全圈数据后台'
